package com.example.good.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.good.service.IWXService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@Service
public class WXServiceImpl implements IWXService {
    @Override
    public Object WXLogin(String code) {
        String appid = "wx4c21f4266e999ee2";
        String secret = "32959499cc99707e209e43aba9f9d2f3";
        String js_code = code;
        String openid = "";
        String session_key = "";

        //自己的微信id和key
//        openid="oQkAA5o4HeXudQtyHFB1E3mlggPs";
//        session_key="TwxHY/uVmTSHGg0EfuyrkQ==";

        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+
                "&secret="+secret+"&js_code="+js_code + "&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
//        if(jsonObject.containsKey("errcode")){
//
//        }
        if(jsonObject.containsKey("openid") && jsonObject.containsKey("session_key")){
            openid = jsonObject.getString("openid");
            session_key = jsonObject.getString("session_key");
            return null;
        }else{
            return jsonObject;
        }
    }
}
